package com.jiuyuejiu;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.application.ConstValues;
import com.jiuyuejiu.base.BaseActivity;
import com.jiuyuejiu.domain.HomePicStc;
import com.jiuyuejiu.func_book.BookListActivity;
import com.jiuyuejiu.func_pic.LocalPicTypeActivity;
import com.jiuyuejiu.func_video.video.fenlei.VideoFenleiActivity;
import com.jiuyuejiu.func_video.video.jiequ.VideoRandomActivity;
import com.jiuyuejiu.httpid.HttpipActivity;
import com.jiuyuejiu.singleimg.HtmlVideoActivity;
import com.jiuyuejiu.utils.ShanHaiUtil;
import com.jiuyuejiu.utils.ViewUtil;
import com.kylin.core.utils.dbtutil.JsonUtil;
import com.kylin.core.utils.flyn.Eyes;

import java.lang.ref.SoftReference;
import java.util.ArrayList;

import androidx.appcompat.widget.AppCompatTextView;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            // .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_gray_5)// 占位图
            .transform(new CenterCrop(), new RoundedCorners(18));

    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private ImageView headphotoImg;
    private AppCompatTextView titleTv;
    private AppCompatTextView username;

    private ImageView img_video;
    private TextView tv_video;
    private ImageView img_random;
    private TextView tv_random;
    private ImageView img_duopbiaoqing;
    private TextView tv_duopbiaoqing;
    private ImageView img_gaoqingdanp;
    private TextView tv_gaoqingdanp;
    private ImageView img_jiequ;
    private TextView tv_jiequ;
    private ImageView img_pic;
    private TextView tv_pic;
    private ImageView img_book;
    private TextView tv_book;
    private ImageView img_html;
    private TextView tv_html;


    private String http_ip;


    private MainService service;
    private MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<MainActivity> fragmentRef;

        public MyHandler(MainActivity fragment) {
            fragmentRef = new SoftReference<MainActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            MainActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();

            // 处理UI 变化
            switch (msg.what) {
                case ConstValues.WAIT0: // 获取首页封面的回调
                    fragment.getHomePicListSuc(bundle);
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 全屏
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_home);

        // 标题栏白底黑字
        Eyes.setStatusBarLightMode(this, Color.WHITE);
        // 初始化视图
        initView();
        // 初始化数据
        initData();
    }

    // 初始化视图
    private void initView() {

        backBtn = (RelativeLayout) findViewById(R.id.main_rl_back);
        headphotoImg = (ImageView) findViewById(R.id.main_img_headphoto);
        username = (AppCompatTextView) findViewById(R.id.main_tv_username);
        confirmBtn = (RelativeLayout) findViewById(R.id.main_rl_confirm);
        confirmTv = (AppCompatTextView) findViewById(R.id.main_bt_confirm);
        titleTv = (AppCompatTextView) findViewById(R.id.main_tv_title);

        backBtn.setVisibility(View.VISIBLE);
        confirmBtn.setVisibility(View.INVISIBLE);
        backBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);

        img_video = findViewById(R.id.main_img_video);
        tv_video = findViewById(R.id.main_tv_video);
        img_random = findViewById(R.id.main_img_random);
        tv_random = findViewById(R.id.main_tv_random);
        img_duopbiaoqing = findViewById(R.id.main_img_duopbiaoqing);
        tv_duopbiaoqing = findViewById(R.id.main_tv_duopbiaoqing);
        img_gaoqingdanp = findViewById(R.id.main_img_gaoqingdanp);
        tv_gaoqingdanp = findViewById(R.id.main_tv_gaoqingdanp);
        img_jiequ = findViewById(R.id.main_img_jiequ);
        tv_jiequ = findViewById(R.id.main_tv_jiequ);
        img_pic = findViewById(R.id.main_img_pic);
        tv_pic = findViewById(R.id.main_tv_pic);
        img_book = findViewById(R.id.main_img_book);
        tv_book = findViewById(R.id.main_tv_book);
        img_html = findViewById(R.id.main_img_html);
        tv_html = findViewById(R.id.main_tv_html);


        img_video.setOnClickListener(this);
        tv_video.setOnClickListener(this);
        img_pic.setOnClickListener(this);
        tv_pic.setOnClickListener(this);
        img_book.setOnClickListener(this);
        tv_book.setOnClickListener(this);
        img_html.setOnClickListener(this);
        tv_html.setOnClickListener(this);
        img_random.setOnClickListener(this);
        tv_random.setOnClickListener(this);
        img_duopbiaoqing.setOnClickListener(this);
        tv_duopbiaoqing.setOnClickListener(this);
        img_gaoqingdanp.setOnClickListener(this);
        tv_gaoqingdanp.setOnClickListener(this);
        img_jiequ.setOnClickListener(this);
        tv_jiequ.setOnClickListener(this);

    }

    // 初始化数据
    private void initData() {

        handler = new MyHandler(MainActivity.this);
        service = new MainService(MainActivity.this, handler);

        titleTv.setText("王者小知识");

        tv_video.setText(ConstValues.VIDEONAME);
        tv_random.setText(ConstValues.VIDEONAME + "随机");
        tv_duopbiaoqing.setText(ConstValues.DUOPBIAOQINGNAMW + "随机");
        tv_gaoqingdanp.setText(ConstValues.GAOQINGDANPNAMW + "随机");
        tv_jiequ.setText(ConstValues.JIEQUNAMW + "随机");
        tv_pic.setText(ConstValues.PICNAME);
        tv_book.setText(ConstValues.BOOKNAMW);
        tv_html.setText(ConstValues.HTMLNAMW);



        // ----↓ 图片高度 2022/6/4 17:30 ↓------------------------------------------------------------------------------------
        // 获取屏幕长宽
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // int screenWidth = dm.widthPixels;
        int screenWidth = (int)(dm.widthPixels/3);
        int screenHeight = (int)(screenWidth/2);
        RelativeLayout.LayoutParams linearParams = new RelativeLayout.LayoutParams(screenWidth,screenHeight);
        img_video.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_pic.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_book.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_html.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_random.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_duopbiaoqing.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_gaoqingdanp.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        img_jiequ.setLayoutParams(linearParams); //使设置好的布局参数应用到控件


        // ----↑ 图片高度 2022/6/4 17:30 ↑------------------------------------------------------------------------------------


        // 头像
        Glide.with(MainActivity.this)
                .load(ConstValues.HEADPHOTO)
                .placeholder(R.mipmap.ic_launcher)// 占位图
                .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
                .into(headphotoImg);

        // 获取首页封面
        service.getHomePicLst(ConstValues.HOME_JSON_PIC);
    }

    // ----↓ 获取首页封面的回调 2022/2/15 3:51 下午 ↓------------------------------------------------------------------------------------
    private void getHomePicListSuc(Bundle bundle) {

        String formjson = bundle.getString("formjson");
        String status = bundle.getString("status");

        if (ConstValues.SUCCESS.equals(status)) {

            ArrayList<HomePicStc> homePicStcs = (ArrayList<HomePicStc>) JsonUtil.parseList(formjson, HomePicStc.class);

            // 设置封面
            setHomePic(homePicStcs);

        } else {
            Toast.makeText(MainActivity.this, formjson, Toast.LENGTH_SHORT).show();
        }
    }
    // ----↑ 获取首页封面的回调 2022/2/15 3:51 下午 ↑------------------------------------------------------------------------------------

    // 设置封面
    private void setHomePic(ArrayList<HomePicStc> homePicStcs) {

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(0).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_video);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(1).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_random);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(2).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_duopbiaoqing);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(3).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_gaoqingdanp);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(4).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_jiequ);

        Glide.with(this)
                .load(homePicStcs.get(5).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_book);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(6).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_pic);

        Glide.with(MainActivity.this)
                .load(homePicStcs.get(7).getImageurl())
                .apply(BANNER_OPTIONS)
                .into(img_html);


    }

    @Override
    public void onClick(View v) {

        // 获取显示的用户名
        // String name = username.getText().toString();
        // 获取服务器ip
        http_ip = ShanHaiUtil.getHttpid();

        switch (v.getId()) {
            case R.id.main_rl_back:// 头像
                if (ViewUtil.isDoubleClick(v.getId(), 800))
                    return;

                Intent httpipAty = new Intent(MainActivity.this, HttpipActivity.class);
                startActivity(httpipAty);

                break;

            // -------------------------------------------------------------------------------------
            case R.id.main_img_video:// 视频教学
                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;

                // Intent videoAty = new Intent(MainActivity.this, VideoFenleiActivity.class);
                Intent videoAty = new Intent(MainActivity.this, VideoFenleiActivity.class);
                videoAty.putExtra("categoryname", ConstValues.VIDEONAME);//
                videoAty.putExtra("title", tv_video.getText().toString());//
                startActivity(videoAty);
                break;

            case R.id.main_img_random:// video随机

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;
                Intent video = new Intent(MainActivity.this, VideoRandomActivity.class);
                video.putExtra("categoryname", ConstValues.VIDEONAME);//
                video.putExtra("title", tv_random.getText().toString());//

                startActivity(video);
                break;
            case R.id.main_img_duopbiaoqing:// 多人标清随机

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;
                Intent duopbiaoqing = new Intent(MainActivity.this, VideoRandomActivity.class);
                duopbiaoqing.putExtra("categoryname", ConstValues.DUOPBIAOQINGNAMW);//
                duopbiaoqing.putExtra("title", tv_duopbiaoqing.getText().toString());//
                startActivity(duopbiaoqing);
                break;
            case R.id.main_img_gaoqingdanp:// 高清单人随机

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;
                Intent gaoqingdanp = new Intent(MainActivity.this, VideoRandomActivity.class);
                gaoqingdanp.putExtra("categoryname", ConstValues.GAOQINGDANPNAMW);//
                gaoqingdanp.putExtra("title", tv_gaoqingdanp.getText().toString());//
                startActivity(gaoqingdanp);
                break;


            case R.id.main_img_jiequ:// 截取视频随机

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;
                Intent jiequ = new Intent(MainActivity.this, VideoRandomActivity.class);
                jiequ.putExtra("categoryname", ConstValues.JIEQUNAMW);//
                jiequ.putExtra("title", tv_jiequ.getText().toString());//

                startActivity(jiequ);
                break;

            case R.id.main_img_pic:// 3d_pic
                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;

                Intent pic = new Intent(MainActivity.this, LocalPicTypeActivity.class);
                pic.putExtra("title", tv_pic.getText().toString());//
                startActivity(pic);

                break;
            case R.id.main_img_book:// 文章攻略

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;

                Intent bookI = new Intent(MainActivity.this, BookListActivity.class);
                bookI.putExtra("title", tv_book.getText().toString());//
                startActivity(bookI);

                break;



            case R.id.main_img_html://

                if (ViewUtil.isDoubleClick(v.getId(), 2500))
                    return;

                Intent html = new Intent(MainActivity.this, HtmlVideoActivity.class);
                html.putExtra("imgurl", ConstValues.WANGZHEYINGDI);// 下载背景图
                html.putExtra("categoryname", ConstValues.VIDEONAME);//
                startActivity(html);

                /*//简单播放一首歌曲
                SongInfo info = new SongInfo();
                info.setSongId("111");
                info.setSongUrl("http://192.168.31.128:8080/landking/mp3/烦死了_1_1.MP3");
                StarrySky.with().playMusicByInfo(info);*/

                break;

        }
    }

    @Override
    public void onBackPressedSupport() {

        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {

            // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
            super.onBackPressedSupport();
            // finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }

}
